package service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import bean.Course;
import bean.CourseBehave;
import bean.Course_Student;
import bean.SingleCourse;
import bean.Student;
import service.SyncService;

@Service @Transactional
public class SyncServiceBean implements SyncService{
	@Resource SessionFactory factory;

	@Override
	public Map<String, Object> recoverData(int tid) {
		Map<String, Object> map = new HashMap<String, Object>();
/*		List<Student> studentlist = new ArrayList<>();
		List<Course> courselist = new ArrayList<>();
		List<SingleCourse> singleCourselist = new ArrayList<>();
		List<CourseBehave> courseBehavelist = new ArrayList<>();
		List<Course_Student> course_Studentlist = new ArrayList<>();*/
	//	Teacher teacher = (Teacher) factory.getCurrentSession().get(Teacher.class, tid);
		//先获取teacher对象
	//	if(teacher != null)		//若成功获取teacher对象后将所有相关数据存入map
	//	{
			//map.put("teacher", teacher);
			map.put("courselist", factory.getCurrentSession().createQuery("from Course where teacher_id = ?")
				.setParameter(0, tid).list());
			map.put("studentlist", factory.getCurrentSession().createQuery("from Student where teacher_id = ?")
				.setParameter(0, tid).list());
			map.put("singlecourselist", factory.getCurrentSession().createQuery("from SingleCourse where teacher_id = ?")
				.setParameter(0, tid).list());
			map.put("coursebehavelist", factory.getCurrentSession().createQuery("from CourseBehave where teacher_id = ?")
				.setParameter(0, tid).list());
			map.put("course_studentlist", factory.getCurrentSession()
				.createQuery("from Course_Student where teacher_id = ?").setParameter(0, tid).list());
	//	}
		return map;
	}

	/**
	 * 更新同步数据的时间
	 */
	@Override
	public void updateSyncTime(int tid, Date synctime) 
	{
		factory.getCurrentSession()
			    .createQuery("update Teacher set tsync_time = ? where id = ?")
				.setParameter(0, synctime)
				.setParameter(1, tid)
				.executeUpdate();		
	}
	
	@Override
	public int backupData(int tid, List<Student> studentlist, List<Course> courselist, List<SingleCourse> singleCourselist,
			List<CourseBehave> courseBehavelist, List<Course_Student> course_Studentlist) {

		try
		{
			for(Student student : studentlist)
			{
				factory.getCurrentSession().merge(student);		//循环更新或添加数据，如果此处用saveOrUpdate会出错
			}
			for(Course course : courselist)
			{
				factory.getCurrentSession().merge(course);
			}
			for(SingleCourse singleCourse : singleCourselist)
			{
				factory.getCurrentSession().merge(singleCourse);
			}
			for(CourseBehave courseBehave : courseBehavelist)
			{
				factory.getCurrentSession().merge(courseBehave);
			}
			factory.getCurrentSession()
		    .createQuery("delete Course_Student where teacher_id = ?")
			.setParameter(0, tid)
			.executeUpdate();	
			//覆盖学生-课程关系表，之前先清除原有信息以防重复添加
			for(Course_Student course_Student : course_Studentlist)
			{
				factory.getCurrentSession().merge(course_Student);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return 1;
	}

}
